﻿///验证是否为数字
function CheckNum(str)
{   
    if(str!= "")
    {
        if(/\d+/.test(str)) 
            return true; 
    }
    return false;
}

function CheckFloat(str)
{
    if (str!="")
    {
        //if(/^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$/.test(str))
        if(/^\d+(\.\d+)?$/.test(str))
            return true;
    }
    return false;
}


// Int
function CheckInputInt(oInput)
{
    if  ('' != oInput.value.replace(/\d/g,''))
    {
        oInput.value = oInput.value.replace(/\D/g,'');
    }
}

//Float
function CheckInputFloat(oInput)
{
    if('' != oInput.value.replace(/\d{1,}\.{0,1}\d{0,}/,''))
    {
        oInput.value = oInput.value.match(/\d{1,}\.{0,1}\d{0,}/) == null ? '' :oInput.value.match(/\d{1,}\.{0,1}\d{0,}/);
    }
}


///去掉字符串前后的空格
String.prototype.Trim = function()
{
     return this.replace(/(^\s+)|\s+$/g,"");
}

ShowVal=function(val)
{
    if(val)
        return val;
    return "";
}

GetIntVal=function(val)
{
    if(val=="" || !CheckNum(val))
       return 0;
    return parseInt(val,10);
}

GetFloatVal=function(val)
{
    if(val=="" || !CheckFloat(val))
       return 0;
    return Math.round(val*Math.pow(10, 2))/Math.pow(10, 2);
}

GetDateVal = function(val)
{
    if(val=="")
       return "";    
    return new Date(val).Format("yyyy-MM-dd");
}

BindSelectByListItems = function(selectObj,selectedVal,ListItems)
{
    if (ListItems)
    {
        for(var i=0, count = ListItems.length; i < count; ++i) {                    
            var opt = new Option(ListItems[i].Text, ListItems[i].Value);
            if (ListItems[i].Value==selectedVal)
                opt.selected=true;
            selectObj.options.add(opt);
        }
    }
}

SetSelected = function(selectObj,selectedVal)
{    
    for(var i=0;i<selectObj.options.length;i++)
        if(selectObj.options[i].value==selectedVal)
            selectObj.options[i].selected=true;
}

//为一个DOM对象的事件附加方法
function AddEvent(element,evts,func){
    if (element.addEventListener){
         element.addEventListener(evts.replace('on',''),func, false);          
    } else if (element.attachEvent){
         element.attachEvent(evts, func);
    } 
}
//移除一个DOM对象事件上附加的方法
function RemoveEvent(element,evts,func){
    if (element.removeEventListener){
         element.removeEventListener(evts.replace('on',''),func, false);          
    } else if (element.attachEvent){
         element.detachEvent(evts, func);
    } 
}

//删除Select的所有option
function RemoveAllOption(obj)
{
    if (obj.options && obj.options.length)
    {
//        for(var i=0;i<obj.options.length;i++)
//        {
//            obj.removeChild(obj.options[i]);
//        }
        for(i=obj.options.length-1 ; i>= 0 ; i--)
            obj.options[i] = null;
    } 
}

function ChangeElmntBgColor(obj)
{
    if (obj)
    {
        obj.style.backgroundColor='#EDF1F8';
    }
}

function DateAdd(date,flag,n) 
{ 
     switch (flag) {   
         case 's' :return new Date(Date.parse(date) + (1000 * n));  
         case 'n' :return new Date(Date.parse(date) + (60000 * n));  
         case 'h' :return new Date(Date.parse(date) + (3600000 * n));  
         case 'd' :return new Date(Date.parse(date) + (86400000 * n));  
         case 'w' :return new Date(Date.parse(date) + ((86400000 * 7) * n));  
         case 'q' :return new Date(date.getFullYear(), (date.getMonth()) + n*3, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());  
         case 'm' :return new Date(date.getFullYear(), (date.getMonth()) + n, date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());  
         case 'y' :return new Date((date.getFullYear() + n), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds());  
     } 
} 

/**
* 时间对象的格式化;
*/
Date.prototype.Format = function(formatStr){
 /*
  * eg:formatStr="YYYY-MM-dd hh:mm:ss";
  */
 var o = {
  "M+" :  this.getMonth()+1,  //month
  "d+" :  this.getDate(),     //day
  "h+" :  this.getHours(),    //hour
  "m+" :  this.getMinutes(),  //minute
  "s+" :  this.getSeconds(), //second
  "q+" :  Math.floor((this.getMonth()+3)/3),  //quarter
  "S"  :  this.getMilliseconds() //millisecond
}
 
   if(/(y+)/.test(formatStr)) {
    formatStr = formatStr.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
   }
 
   for(var k in o) {
    if(new RegExp("("+ k +")").test(formatStr)) {
      formatStr = formatStr.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
    }
   }
 return formatStr;
}

function GetQueryString(para)
{
	var retval="";
	var s=location.search.replace("?","");
	if(s=="")return "";s = s.split("&");
	for(var i=0;i<s.length;i++)
	if(s[i].toUpperCase().indexOf(para.toUpperCase() + "=")==0)
	retval+=((retval==""?"":", ")+s[i].substr(s[i].indexOf("=")+1,s[i].length));
	return retval;
}

//让firefox支持‘outerHTML’
if(typeof(HTMLElement)!="undefined" && !window.opera) 
{ 
    HTMLElement.prototype.__defineGetter__("outerHTML",function() 
    { 
        var a=this.attributes, str="<"+this.tagName, i=0;for(;i<a.length;i++) 
        if(a[i].specified) 
            str+=" "+a[i].name+'="'+a[i].value+'"'; 
        if(!this.canHaveChildren) 
            return str+" />"; 
        return str+">"+this.innerHTML+"</"+this.tagName+">"; 
    }); 
    HTMLElement.prototype.__defineSetter__("outerHTML",function(s) 
    { 
        var r = this.ownerDocument.createRange(); 
        r.setStartBefore(this); 
        var df = r.createContextualFragment(s); 
        this.parentNode.replaceChild(df, this); 
        return s; 
    }); 
    HTMLElement.prototype.__defineGetter__("canHaveChildren",function() 
    { 
        return !/^(area|base|basefont|col|frame|hr|img|br|input|isindex|link|meta|param)$/.test(this.tagName.toLowerCase()); 
    }); 
}

function SelectTextRange(obj,sIndex,eIndex){

    var endIndex =(eIndex?eIndex:obj.value.length);
 
    if(window.opera)
    {  
        var rng = obj.createTextRange();//获取一个需要操作对象的TextRange
        rng.collapse();//默认true收缩
        rng.select();//因为已收缩Range，所以select后会将光标移到首部
        
        var sel = document.selection.createRange();//如果需要选中文本中的一部分，需要创建Range
        sel.moveStart("character",sIndex);//移动开始点
        sel.moveEnd("character",endIndex);//移动结束点
        sel.select();//选中开始点到结束点间的字符  
    }
    else
    {
        obj.selectionStart=sIndex;
        obj.selectionEnd =endIndex;
    }
}

function OpenWindowWithPost(url,name,keys,values)
{
    var newWindow = window.open(url, name);
    if (!newWindow) return false;
    var html = "";
    html += "<html><head></head><body><form id='formid' method='post' action='" + url + "'>";
    if (keys && values && (keys.length == values.length))
    for (var i=0; i < keys.length; i++)
    html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";
    html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit()</script></body></html>";
    newWindow.document.write(html);
    return newWindow;
}

/*删除列，采用deleteCell(列位置)的方法*/
function RemoveCell(tbObj,rowIndex,cellIndex){
　　tbObj.rows[rowIndex].deleteCell(cellIndex); 
}

/*document.getElementById("table").rows.item(0).cells.length 
用来获得表格的列数
*/
function AddCell(tbObj,rowIndex,insertPos){
    tbObj.rows[rowIndex].insertCell(insertPos);
}

function ShowChoseProduct(priceMode)
{
    window.open("Productlist.aspx?key=hProducts&PriceMode=" + priceMode,"","width=700,height=300,scrollbars=1,location=0,status=0,resizable=1");    
}

function ShowChoseUser(obj,isAssessor)
{
    window.open("userlist.aspx?key=" + obj.id + "&IsAssessor=" + isAssessor,"","width=500,height=400,scrollbars=1,location=0,status=0,resizable=1");  
}

function ShowBalanceList(obj,tradeType)
{
    location.href="balancelist.aspx?TradeType=" + tradeType + "&TraderId=" + obj.value;
}
